<?php
require_once('system.php');
/*
function getImage($url,$filename='') {
    if(!$url) return false;
    if(!$filename) {
    $ext=strrchr(strtolower($url),'.') ;
    if($ext!='.gif' && $ext!='.jpg' && $ext!='.png') return false ;
    $str=explode('/',$url) ;
    $filename=$str[count($str)-1] ;
    //print $filename.�\n�;
    }

    ob_start();
    readfile($url);
    $img = ob_get_contents();
    ob_end_clean();
    $fp2=fopen($filename, 'a+') ;
    //file_put_contents($filename,$img);
    fwrite($fp2,$img);
    fclose($fp2) ;

    return $filename;
}
getImage('http://www.google.com.hk/intl/zh-CN/images/logo_cn.png',"testpic.png");
*/
if (!empty($_POST['imgarr'])||!empty($_POST['imgsingle'])){
	$urlarr = $_POST["imgarr"];
	$urlsingle = $_POST["imgsingle"];
}else{
	$url ="";
}
require_once('Download.php');
//$url ="http://www.w3school.com.cn/i/site_bg.gif";
//$url ='http://www.google.com.hk/intl/zh-CN/images/logo_cn.png';
//$url = 'http://www.google.com.hk/intl/zh-CN/images/logo_cn.png';
//$urlstring = '<img src="http://www.google.com.hk/intl/zh-CN/images/logo_cn.png" alt="http://www.google.com.hk/intl/zh-CN/images/logo_cn.png">';
$savedFilename = "downloadTest/";
$typearr = array("png","jpg","jpeg","gif");
$download = new Download();
$download->setFileType($typearr,true);
//exec the download command 
if (trim($urlarr)){
	$download->download($urlarr,$savedFilename);
}
if(trim($urlsingle)){
	$download->doDownloadFiles($urlsingle,$savedFilename);
}
if(trim($urlarr)==""&&trim($urlsingle)==""){
	$Log = new Log();
	$Log->write("url is empty.");
	$Log->writeExcepLog("url is empty");
	$download->setError("url is empty");
	$Log->xmlTest();
}
//print the download message;
$errors = $download->getErrors();
$messages = $download->getMessage();
//var_dump();
if (empty($errors)&&!empty($messages)){
	foreach ($messages as $key=>$message){
		if (is_array($message)){
			echo $key."=>";
			foreach ($message as $key2=>$message2){
				echo "$key2"."=>".$message2;
				echo "<br />";
			}
			echo "<br />";
		}else {
			echo "$key"."=>".$message;
			echo "<br />";
		}
	}
	//display the image
	$urls = $download->getMessage();
	$urls = $urls["url"];
	foreach ($urls as $url){
		echo "<img src='$url' />";
	}
}else {
	foreach ($errors as $error){
		echo $error."<br />";
	}
}

?>